package com.guchenbo.spark.sql;

//import cn.tongdun.chaos.client.enums.DataSourceType;
//import cn.tongdun.chaos.jdbc.autoconfigure.ChaosJdbcProperties;
//import cn.tongdun.chaos.jdbc.util.ChaosJdbcUtils;
//import com.alibaba.druid.pool.DruidDataSource;
//import com.alibaba.druid.pool.DruidDataSourceFactory;
//import java.sql.Connection;
//import java.util.Properties;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;

/**
 * @author guchenbo
 * @date 2022/3/18
 */
public class DruidTest {
//
//    public static void main(String[] args) throws Exception {
//        ExecutorService executor = Executors.newFixedThreadPool(10);
//
//        ChaosJdbcProperties jdbcProperties = new ChaosJdbcProperties();
//        jdbcProperties.setInitialSize(2);
//        jdbcProperties.setMaxWait(2000);
//        String url = "jdbc:mysql://127.0.0.1:3306";
//        String username = "gcb";
//        String password = "guchenbo";
//        String driver = DataSourceType.DATASOURCE_TYPE_MYSQL.getDriverName();
//        Properties properties = ChaosJdbcUtils.buildProperty(url, username, password, driver, jdbcProperties);
//        DruidDataSource dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
//
//        //        dataSource.setConnectionErrorRetryAttempts(2);
//        //        dataSource.setBreakAfterAcquireFailure(true);
//
//        dataSource.setMaxActive(3);
//        //        多久检测空闲连接
//        dataSource.setTimeBetweenEvictionRunsMillis(3000);
//        dataSource.setMinEvictableIdleTimeMillis(2000);
//        System.out.println(dataSource.getConnection());
//
//        //        Uninterruptibles.sleepUninterruptibly(5, TimeUnit.MINUTES);
//
//        Thread thread = new Thread(() -> {
//            while (true) {
//                try {
//                    executor.submit(() -> {
//                        try {
//                            Connection connection = dataSource.getConnection();
//                            System.out.println("get conn");
//                            Thread.sleep(15000);
//                            connection.close();
//                            System.out.println("close conn");
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    });
//                    Thread.sleep(3000);
//
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//        thread.setName("get conn");
//        thread.start();
//    }

}
